﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace atmsystem_ss
{
    public partial class frmAdministrator : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dtCustomer = new DataTable("tblCustomer");
        private DataTable dtAccount = new DataTable("tblAccount");
        private DataTable dtTransaction = new DataTable("tblTransaction");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong
        }
        private void getdataCustomer()
        {
            SqlCommand command = new SqlCommand();// khai bao 1 command
            command.Connection = con; //ket noi
            command.CommandType = CommandType.Text;//khai bao kieu command
            command.CommandText = @"Select  fldID_Customer as N'CustomerID',
                                            fldFull_Name as N'FullName',
                                            fldID_Number as N'IDNumber',
                                            (Case fldSex when 'True' then N'Male' else N'Female' end) as N'Sex',
                                            fldBirth as N'Birth',
                                            fldAddress as N'Address',
                                            fldPhone as N'Phone',
                                            fldEmail as N'Email'
                                            From tblCustomer";
            da.SelectCommand = command;//gan commad cho da
            da.Fill(dtCustomer);//nap du lieu cho table
            grvdataCustomer.DataSource = dtCustomer;//load du lieu len datagridview
        }
        private void bindingCustomer()
        {
            txtID_Customer_IC.DataBindings.Clear();
            txtID_Customer_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "CustomerID");
            txtFull_Name_IC.DataBindings.Clear();
            txtFull_Name_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "FullName");
            txtID_Number_IC.DataBindings.Clear();
            txtID_Number_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "IDNumber");
            cboSex_IC.DataBindings.Clear();
            cboSex_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "Sex");
            txtBirth_IC.DataBindings.Clear();
            txtBirth_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "Birth");
            txtAddress_IC.DataBindings.Clear();
            txtAddress_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "Address");
            txtPhone_IC.DataBindings.Clear();
            txtPhone_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "Phone");
            txtEmail_IC.DataBindings.Clear();
            txtEmail_IC.DataBindings.Add("Text", grvdataCustomer.DataSource, "Email");

        }
        private void ClearTextBox_IC()
        {
            txtID_Customer_IC.Clear();
            txtFull_Name_IC.Clear();
            txtID_Number_IC.Clear();
            cboSex_IC.Text="";
            txtBirth_IC.Clear();
            txtAddress_IC.Clear();
            txtPhone_IC.Clear();
            txtEmail_IC.Clear();
        }
        private bool tontai_IC()
        {
            dtCustomer.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblCustomer where (fldID_Customer like @fldID_Customer_IC) ";
            command.Parameters.Add("@fldID_Customer_IC", SqlDbType.NVarChar, 50).Value = txtID_Customer_IC.Text;
            da.SelectCommand = command;
            da.Fill(dtCustomer);
            if (dtCustomer.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private bool isValidEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }
        private bool IsDateTime(string txtDate)
        {
            DateTime tempDate;
            return DateTime.TryParse(txtDate, out tempDate) ? true : false;
        }
        private bool kiemtra_IC()
        {
            kt = true;
            if (txtID_Customer_IC.Text == "")
            {
                txtID_Customer_IC.Focus();
                MessageBox.Show("Customer ID not null!", "Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (tontai_IC() == false)
            {
                txtID_Customer_IC.Text = "";
                txtID_Customer_IC.Focus();
                MessageBox.Show("Customer ID is exists, Re-Enter Customer!", "Error", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtFull_Name_IC.Text == "")
            {
                txtFull_Name_IC.Focus();
                MessageBox.Show("Full Name not null!", " Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtID_Number_IC.Text == "")
            {
                txtID_Number_IC.Focus();
                MessageBox.Show("ID Number not null!", " Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (cboSex_IC.Text == "")
            {
                cboSex_IC.Focus();
                MessageBox.Show("Sex not null!", " Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtBirth_IC.Text == "")
            {
                txtBirth_IC.Focus();
                MessageBox.Show("Birth not null!", " Error", MessageBoxButtons.OK);
                kt = false;
            }
                else if (IsDateTime(txtBirth_IC.Text.ToString())==false)
            {
                txtBirth_IC.Focus();
                MessageBox.Show("The format invalid!", " Error", MessageBoxButtons.OK);
                kt = false;
                }
            else if (txtAddress_IC.Text == "")
            {
                txtAddress_IC.Focus();
                MessageBox.Show("Address not null!", "Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtPhone_IC.Text == "")
            {
                txtPhone_IC.Focus();
                MessageBox.Show("Phone not null!", " Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtEmail_IC.Text == "")
            {
                txtEmail_IC.Focus();
                MessageBox.Show("Email not null!", " Error", MessageBoxButtons.OK);
                kt = false;

            }
            else if (isValidEmail(txtEmail_IC.Text.ToString()) == false)
            {
                txtEmail_IC.Focus();
                MessageBox.Show("The format invalid!", " Error", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }
        public frmAdministrator()
        {
            InitializeComponent();
            

        }

        private void frmAdministrator_Load(object sender, EventArgs e)
        {
            connect();
            getdataCustomer();
            bindingCustomer();
        }

        private void btnNew_IC_Click(object sender, EventArgs e)
        {
            ClearTextBox_IC();
            btnUpdate_IC.Enabled = false;
            btnDelete_IC.Enabled = false;
            btnExit_IC.Enabled= false;
        }

        private void btnSave_IC_Click(object sender, EventArgs e)
        {
            kiemtra_IC();
            if (kt == true)
            {
                DataRow row = dtCustomer.NewRow();
                row["fldID_Customer"] = txtID_Customer_IC.Text;
                row["fldFull_Name"] = txtFull_Name_IC.Text;
                row["fldID_Number"] = txtID_Number_IC.Text;
                if (cboSex_IC.Text == "Male")
                    row["fldSex"] = true;
                else if (cboSex_IC.Text == "Female")
                    row["fldSex"] = false;
                row["fldBirth"] = txtBirth_IC.Text;
                row["fldAddress"] = txtAddress_IC.Text;
                row["fldPhone"] = txtPhone_IC.Text;
                row["fldEmail"] = txtEmail_IC.Text;
                dtCustomer.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert tblCustomer (fldID_Customer, fldFull_Name, fldID_Number, fldSex, fldBirth, fldAddress, fldPhone, fldEmail) Values (@fldID_Customer, @fldFull_Name, @fldID_Number, @fldSex, @fldBirth, @fldAddress, @fldPhone, @fldEmail)";
                commandInsert.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50, "CustomerID");
                commandInsert.Parameters.Add("@fldFull_Name", SqlDbType.NVarChar, 50, "Fullname");
                commandInsert.Parameters.Add("@fldID_Number", SqlDbType.NVarChar, 50, "IDNumber");
                commandInsert.Parameters.Add("@fldSex", SqlDbType.Bit, 50, "Sex");
                commandInsert.Parameters.Add("@fldBirth", SqlDbType.DateTime, 50, "Birth");
                commandInsert.Parameters.Add("@fldAddress", SqlDbType.NVarChar, 50, "Address");
                commandInsert.Parameters.Add("@fldPhone", SqlDbType.NVarChar, 50, "Phone");
                commandInsert.Parameters.Add("@fldEmail", SqlDbType.NVarChar, 50, "Email");
                da.InsertCommand = commandInsert;
                da.Update(dtCustomer);
                getdataCustomer();
                ClearTextBox_IC();
                MessageBox.Show("Insert successed", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
        }
      
    }
}
